package com.zhoug.zmui3.group;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import com.zhoug.common3.utils.LogUtils;
import com.zhoug.zmui3.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import androidx.annotation.Nullable;

/**
 * 流动布局,childView水平排列,自动换行换行,
 * <p>
 * TextView android:gravity="center"无效时要设置android:singleLine="true"
 *
 * @Author 35574
 * @Date 2020/11/2
 * @Description
 */
@Deprecated
public class ZMUIFlowViewGroup1 extends ViewGroup {
    private static final String TAG = ">>>ZMUIFlowViewGroup";
    private static final boolean DEBUG = false;
    /**
     * 存储每一行
     */
    private List<WrapLine> mWrapLineGroup = new ArrayList<>();

    /**
     * 对齐方式 right 0，left 1，center 2
     * {@link Gravity#START,Gravity#END,Gravity#CENTER_HORIZONTAL}
     */
    private int gravity;
    /**
     * 水平间距,单位px
     */
    private int horizontalSpace;
    /**
     * 垂直间距,单位px
     */
    private int verticalSpace;
    /**
     * 是否自动填满
     * 充满当前行时,每个ChildView平分剩余的空间
     */
    private boolean isFull;

    public ZMUIFlowViewGroup1(Context context) {
        this(context, null);
    }

    public ZMUIFlowViewGroup1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZMUIFlowViewGroup1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }


    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZMUIFlowViewGroup, defStyleAttr, R.style.zmui_style_ZMUIFlowViewGroup);
        gravity = a.getInt(R.styleable.ZMUIFlowViewGroup_android_gravity, Gravity.START);
        horizontalSpace = a.getDimensionPixelOffset(R.styleable.ZMUIFlowViewGroup_zmui_horizontalSpace, 0);
        verticalSpace = a.getDimensionPixelOffset(R.styleable.ZMUIFlowViewGroup_zmui_verticalSpace, 0);
        isFull = a.getBoolean(R.styleable.ZMUIFlowViewGroup_zmui_isFull, false);

        a.recycle();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        /**
         * 计算宽度
         */
        int width = 0;
        switch (widthMode) {
            case MeasureSpec.EXACTLY://确定的宽度
                width = widthSize;
                break;
            case MeasureSpec.AT_MOST://限制最大宽度
                //取所有子View排一行所需的宽度和最大宽度的最小值为实际宽度
                width = Math.min(width, getRequestWidth());
                break;
            case MeasureSpec.UNSPECIFIED://想要多宽有多宽
                //取所有子View排一行所需的宽度为实际宽度
                width = getRequestWidth();
                break;
        }

        /**
         * 根据宽度计算出需要的行数
         */
        mWrapLineGroup.clear();
        WrapLine wrapLine = new WrapLine();
        mWrapLineGroup.add(wrapLine);
        int childCount = getChildCount();
        View childView = null;
        if (DEBUG) {
            LogUtils.d(TAG, "onMeasure:width=" + width);
        }
        for (int i = 0; i < childCount; i++) {
            childView = getChildAt(i);
            //当前子View排在同一行时所需宽度大于了容器宽度
            //非本行的第一个View则换行后添加到新行,第一个View则添加到本行后再换新行
            int childWidth = childView.getMeasuredWidth();
            if (DEBUG) {
                LogUtils.d(TAG, String.format(Locale.CANADA,"childWidth%d=%d",i,childWidth));
            }
            int w = wrapLine.lineWidth +childWidth ;
            if (DEBUG) {
                LogUtils.d(TAG, "onMeasure:w=" + w);
            }
            if (w > width) {
                if (wrapLine.lineViews.size() == 0) {
                    wrapLine.addView(childView);
                    //另起一行
                    wrapLine = new WrapLine();
                    mWrapLineGroup.add(wrapLine);
                } else {
                    //先换行
                    wrapLine = new WrapLine();
                    mWrapLineGroup.add(wrapLine);
                    wrapLine.addView(childView);
                }
            } else {
                wrapLine.addView(childView);
            }
        }

        /**
         * 计算高度
         */
        int height = getPaddingTop() + getPaddingBottom();
        for (int i = 0; i < mWrapLineGroup.size(); i++) {
            if (i != 0) {
                //非第一个的顶部加上间隔
                height += verticalSpace;
            }
            height += mWrapLineGroup.get(i).lineHeight;
        }
        switch (heightMode) {
            case MeasureSpec.EXACTLY:
                height = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                height = Math.min(heightSize, height);
                break;
            case MeasureSpec.UNSPECIFIED:
                break;

        }
        setMeasuredDimension(width, height);
    }

    /**
     * 获取需求的最大宽度
     *
     * @return
     */
    private int getRequestWidth() {
        //取所有子View排一行所需的宽度为最大需求宽度
        int width = getPaddingStart() + getPaddingEnd();
        int childCount = getChildCount();
        View childView;
        for (int i = 0; i < childCount; i++) {
            childView = getChildAt(i);
            if (i != 0) {
                width += horizontalSpace;
            }
            width += childView.getMeasuredWidth();
        }
        return width;
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        WrapLine wrapLine;
        int top = getPaddingTop();
        int left;
        View childView;
        int lineChildSize;
        for (int raw = 0; raw < mWrapLineGroup.size(); raw++) {
            wrapLine = mWrapLineGroup.get(raw);
            lineChildSize = wrapLine.lineViews.size();
            //一行剩余的空间
            int freeWidth = getMeasuredWidth() - wrapLine.lineWidth;
            if (isFull) {
                //充满当前行时,每个ChildView平分剩余的空间
                left = getPaddingStart();
                for (int column = 0; column < lineChildSize; column++) {
                    childView = wrapLine.lineViews.get(column);
                    childView.layout(left, top, left + childView.getMeasuredWidth() + freeWidth / lineChildSize, top + childView.getMeasuredHeight());
                    left += childView.getMeasuredWidth() + freeWidth / lineChildSize + horizontalSpace;
                }
            } else {
                if (gravity == Gravity.CENTER_HORIZONTAL) {
                    if (DEBUG) {
                        LogUtils.d(TAG, "onLayout:CENTER_HORIZONTAL");
                    }
                    left = getPaddingStart() + freeWidth / 2;
                } else if (gravity == Gravity.END) {
                    if (DEBUG) {
                        LogUtils.d(TAG, "onLayout:END");
                    }
                    left = getPaddingStart() + freeWidth;
                } else {
                    if (DEBUG) {
                        LogUtils.d(TAG, "onLayout:START");
                    }
                    left = getPaddingStart();
                }
                for (int column = 0; column < lineChildSize; column++) {
                    childView = wrapLine.lineViews.get(column);
                    childView.layout(left, top, left + childView.getMeasuredWidth(), top + childView.getMeasuredHeight());
                    left += childView.getMeasuredWidth() + horizontalSpace;
                }

            }
            top += wrapLine.lineHeight + verticalSpace;
        }

    }

    /**
     * 用于存放一行的子View
     */
    private class WrapLine {
        /**
         * 同一行的子View
         */
        private List<View> lineViews = new ArrayList<>();
        ;
        /**
         * 当前行所需占用的宽度
         */
        private int lineWidth;
        /**
         * 当前行所需的最大高度即最高View的高度
         */
        private int lineHeight;

        public WrapLine() {
            lineWidth = getPaddingStart() + getPaddingEnd();
        }

        private void addView(View view) {
            if (lineViews.size() != 0) {
                //不是一行的第一个View,
                lineWidth += horizontalSpace;
            }
            lineWidth += view.getMeasuredWidth();
            lineHeight = Math.max(lineHeight, view.getMeasuredHeight());
            lineViews.add(view);
        }

    }


}
